/**
 * Created by Esri on 2016/6/28.
 */
define([ "dojo/_base/declare",
        "gistech/widgets/baseWidget/BaseWidget",
        "gistech/widgets/ToolBox/MeasureLength",
        "dojo/_base/lang",
        "dojo/on",
        "dojo/dom-construct",
        "dojo/query",
        "dojo/dom-style",
        "dojo/dom-class",
        "dojo/dom-attr",
        "dojo/topic",
        "esri/layers/FeatureLayer",
        "esri/symbols/PictureMarkerSymbol",
        "esri/layers/ArcGISTiledMapServiceLayer",
        "esri/layers/ArcGISDynamicMapServiceLayer",
        "../../util/ajaxUtil",
        "dojo/text!./templates/Template.html"],
    function(declare,BaseWidget,MeasureLength,lang,on,domConstruct,query, 
    		domStyle,domClass,domAttr,topic,FeatureLayer,PictureMarkerSymbol,ArcGISTiledMapServiceLayer,ArcGISDynamicMapServiceLayer,ajaxUtil,template) {
	
	    var codes = [];
        return declare([BaseWidget], {
            templateString : template,
            map:null,
            zoom:0,
            isopen:false,
            borderTiledLayer:null,
            borderDynamicLayer:null,
            
            constructor : function(args) {
                this.inherited(arguments);
                this.map=args.map;
                this.showBorder = true;
                this.container = args.container;
            },
            
            postMixInProperties:function () {
                this.inherited(arguments);
            },
            
            postCreate : function() {
            	this.init();
                this.bindEvent();
                this.inherited(arguments);
            },
            
            startup:function(){
                this.inherited(arguments);
            },
            
            destroy:function(){
                this.inherited(arguments);
            },
            
            init:function(){	
            	domConstruct.place(this.domNode, this.container);
            },
            
            bindEvent:function(){
            	topic.subscribe("toggleToolBox", lang.hitch(this, this.toggleToolBox));
            	topic.subscribe("clearborderfeaturelayers", lang.hitch(this, this.ClearFtLayers));
            	
            	on(this.mapbtn, "click", lang.hitch(this, this.MapBtnClick));
            	on(this.drawbtn, "click", lang.hitch(this, this.DrawBtnClick));
            	on(this.toolbtn, "click", lang.hitch(this, this.ToolBtnClick));
            	
            },
            
            MapBtnClick:function(){
            	var _this=this;
            	this.map.setMapCursor("url(gistech/widgets/ManageTools/images/cursor_linemeasure.cur),auto");
            	MeasureLength.action(_this.map);
            },
            
            DrawBtnClick:function(){
            	topic.publish("showDrawGreaphic", '');
            },
            
            
            
            TopBtnClick:function(){
            	ajaxUtil.request("../client/service/GetStoreIDController/getStoreID","",lang.hitch(this,function(result){
            		console.log(result)  
            		var options={
                		mode: esri.layers.FeatureLayer.MODE_ONDEMAND, 
                		outFields: ["*"]
                	}
            		var SimpleSymbol1 = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 15, 
                			new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
                					new dojo.Color([255, 255, 0]), 0.95), new dojo.Color([255,0,0, 0.95]));
                	
                	var SimpleSymbol2 = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 15, 
                			new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
                					new dojo.Color([0,0,255]), 0.25), new dojo.Color([0,255,0, 0.25]));
            		var symbol1 = new PictureMarkerSymbol({
                	    "url":"gistech/widgets/ToolBox/images/a.png",
                	    "height":15,
                	    "width":15,
                	    "type":"esriPMS"
                	  });
            		var symbol2 = new PictureMarkerSymbol({
                	    "url":"gistech/widgets/ToolBox/images/b.png",
                	    "height":15,
                	    "width":15,
                	    "type":"esriPMS"
                	  });
            		
            		var renderer = new esri.renderer.UniqueValueRenderer(SimpleSymbol2, "STOREID");
            		for(var i=0;i<result.length;i++){
            			console.log(result[i].storeID)
            			var strID=result[i].storeID+"";
            			renderer.addValue(strID, SimpleSymbol1);
            		}  
            		var ftLayer=new FeatureLayer(ioc.map.TCPUrl+"/6",options);
                	ftLayer.setRenderer(renderer);
                	this.map.addLayer(ftLayer);
                 }),function(){
                    console.error("TCP要素点");
                 });
            },
            
            ToolBtnClick:function(){
//            	alert("ToolBtnClick");
//            	window.location="client/gitstech/widgets/Help/help.html";
            	window.open("../client/gistech/widgets/Help/help.html");
            },
            
            toggleToolBox:function(){
            	if(this.isopen){
            		domClass.remove(this.domNode, "showtoolanimation");
            		domClass.add(this.domNode, "hiddentoolanimation");
            		//domClass.add(dojo.byId("drawGraphic"), "hiddentoolanimation");
            		Global.drawGraphic.hideBox();//隐藏toolbox的同时隐藏框选组件
            	}else{
            		domClass.remove(this.domNode, "hiddentoolanimation");
            		domClass.add(this.domNode, "showtoolanimation");
            	}
            	this.isopen=!this.isopen;
            },
            ClearFtLayers:function(){
            	this.map.removeLayer(this.borderDynamicLayer);
        		this.map.removeLayer(this.borderTiledLayer);
        		this.showBorder = !this.showBorder;
            }
        })
    }
);